use rust_mcp_sdk::macros::{mcp_tool, JsonSchema};
use rust_mcp_sdk::schema::{schema_utils::CallToolError, CallToolResult, TextContent};

use crate::tms_client::TmsClient;

#[mcp_tool(
    name = "get_system_stats",
    title = "Get System Statistics",
    description = "Get system statistics including vehicle counts, order counts, and performance metrics.",
    destructive_hint = false,
    idempotent_hint = true,
    open_world_hint = false,
    read_only_hint = true
)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema)]
pub struct GetSystemStatsTool {}

impl GetSystemStatsTool {
    pub async fn run_tool(_params: Self, client: &mut TmsClient) -> Result<CallToolResult, CallToolError> {
        match client.get_system_stats().await {
            Ok(stats) => {
                Ok(CallToolResult::text_content(vec![TextContent::from(format!("{:?}", stats))]))
            },
            Err(e) => Err(CallToolError::new(std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to get system stats: {}", e)))),
        }
    }
}

#[mcp_tool(
    name = "health_check",
    title = "Health Check",
    description = "Perform a health check on the TMS system to verify all services are running properly.",
    destructive_hint = false,
    idempotent_hint = true,
    open_world_hint = false,
    read_only_hint = true
)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, JsonSchema)]
pub struct HealthCheckTool {}

impl HealthCheckTool {
    pub async fn run_tool(_params: Self, client: &mut TmsClient) -> Result<CallToolResult, CallToolError> {
        match client.health_check().await {
            Ok(health) => {
                Ok(CallToolResult::text_content(vec![TextContent::from(format!("{:?}", health))]))
            },
            Err(e) => Err(CallToolError::new(std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to perform health check: {}", e)))),
        }
    }
}